package org.marsdonne.audits.artemis;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.marsdonne.audits.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

@EnableJms
@Configuration
@ConditionalOnProperty(prefix = "audits", name = "artemis.broker-url")
public class ContractConfigurer {

    @Value("${audits.artemis.broker-url:tcp://localhost:61616}")
    private String brokerUrl;

    @Bean
    public ConnectionFactory logsJmsConnectionFactory() {
        return new ActiveMQConnectionFactory(brokerUrl);
    }

    @Bean
    public JmsTemplate auditsJmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(logsJmsConnectionFactory());
        return jmsTemplate;
    }

    @Bean
    public ExceptionAuditable exceptionAudit() {
        return new ExceptionAuditImpl();
    }

    @Bean
    public ChangeAuditable changeAudit() {
        return new ChangeAuditImpl();
    }

    @Bean
    public AuthAuditable authAudit() {
        return new AuthAuditImpl();
    }

    @Bean
    public NoteAuditable noteAudit() {
        return new NoteAuditImpl();
    }

    @Bean
    public OperationAuditable operationAudit() {
        return new OperationAuditImpl();
    }
}
